package com.iprojectmanager.managers;

//import com.iprojectmanager.entities.*;
import javax.persistence.*;
import java.util.*;

/**
 * @author Alucard
 */
public class IProjectManager
{
    protected EntityManagerFactory emf;
    public static final int INSERT = 0;
    public static final int UPDATE = 1;
    public static final int DELETE = 2;
    
    public IProjectManager()
    {
        emf = Persistence.createEntityManagerFactory("iProjectManagerPU");
        //emf.createEntityManager();
    }
 
    public void exec(Object user,int command)
    {
        EntityManager em = emf.createEntityManager();
            try
            {
                em.getTransaction().begin();
                    switch(command)
                    {
                        case INSERT:{
                                em.persist(user);
                                break;}
                        case UPDATE:{
                                //em.refresh(user);
                                user = em.merge(user);
                                break;}
                        case DELETE:{
                                user = em.merge(user);
                                em.remove(user);
                                break;}
                    }
                em.getTransaction().commit();
            }
            catch(Exception ex)
            {
               System.out.println(ex.getMessage());
               em.getTransaction().rollback();
            }
            finally
            {
                if (em != null)
                {
                    em.close();
                }
            }
    }
    
    public List select()
    {
        EntityManager em = emf.createEntityManager();

        return em.createQuery("Select x From Usuario x").getResultList();
    }
}
